﻿using System.Collections;

namespace Gonte.Web.Json
{
    /// <summary>
    /// Contains the data to generate a grid on the client by send it in JSON format
    /// </summary>
    public class JsonGridData
    {
        #region Properties

        /// <summary>
        /// The grid metadata
        /// </summary>
        public JsonGridMetaData metaData { get; set; }

        /// <summary>
        /// The actual data
        /// </summary>
        public ICollection gridData { get; set; } 

        #endregion

        #region Methods

        /// <summary>
        /// Populate this grid using reflection
        /// </summary>
        /// <param name="obj">The object to populate the grid from</param>
        public void Populate(object obj)
        {
            metaData = new JsonGridMetaData();
            metaData.Populate(obj.GetType());

            PopulateGridData(obj);
        } 

        #endregion

        #region Helpers

        private void PopulateGridData(object obj)
        {
            if (obj is ICollection)
            {
                gridData = obj as ICollection;
            }
            else
            {
                gridData = new object[] { obj };
            }
        }

        #endregion
        
    }
}
